package com.example.chatchatbackend.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.chatchatbackend.entity.RestBean;
import com.example.chatchatbackend.entity.dto.Materials;
import com.example.chatchatbackend.entity.vo.response.MaterialsVO;
import com.example.chatchatbackend.entity.vo.response.MaterialsVO;
import com.example.chatchatbackend.service.MaterialsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Slf4j
@RestController
@RequestMapping("/api/materials")
public class MaterialsController {

    @Autowired
    private MaterialsService materialsService;

    @GetMapping
    public RestBean<List<MaterialsVO>> getAllComment(){
        return RestBean.success(materialsService.getAllMaterials());
    }

    @PostMapping
    public RestBean<MaterialsVO> insertMaterials(@RequestBody MaterialsVO materialsVO){
        MaterialsVO insertMaterialsVO = materialsService.insertMaterials(materialsVO);
        return RestBean.success(insertMaterialsVO);

    }

    @PostMapping("/list")
    public RestBean<List<MaterialsVO>> insertMaterialsList (@RequestBody List<MaterialsVO> materialsVO){
        List<MaterialsVO> MaterialsList = materialsService.insertMaterialsList(materialsVO);
        return RestBean.success(MaterialsList);
    }

    @GetMapping("/pageThree")
    public RestBean<IPage<Materials>> pageThree(@RequestParam(value = "pageNum" ,defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        return RestBean.success(materialsService.pageThree(pageNum, pageSize));
    }

    @PutMapping
    public RestBean<MaterialsVO> updateMaterials(@RequestBody MaterialsVO materialsVO){
        return RestBean.success(materialsService.updateMaterials(materialsVO));

    }

    @DeleteMapping("/ids")
    public RestBean<String> delete(@RequestBody List<Integer> ids) {
        materialsService.deleteByIds(ids);
        return RestBean.success("删除成功");
    }

    @GetMapping("/ids")
    public RestBean<List<MaterialsVO>> findByIds(@RequestBody List<Integer> ids){
        List<MaterialsVO> materialsVOList = materialsService.findByIds(ids);
        return RestBean.success(materialsVOList);
    }

    @GetMapping("/name")
    public RestBean<List<MaterialsVO>> findByTitle(@RequestBody String title ){
        List<MaterialsVO> materialsVOList = materialsService.findByTitle(title);
        return RestBean.success(materialsVOList);
    }
}
